package com.lvbaoins.Harmony.quartz.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lvbaoins.Harmony.base.pojo.page.LayuiPageFactory;
import com.lvbaoins.Harmony.core.base.controller.BaseController;
import com.lvbaoins.Harmony.core.reqres.response.ErrorResponseData;
import com.lvbaoins.Harmony.core.reqres.response.ResponseData;
import com.lvbaoins.Harmony.core.util.ToolUtil;
import com.lvbaoins.Harmony.model.exception.ServiceException;
import com.lvbaoins.Harmony.quartz.domain.SysJobLog;
import com.lvbaoins.Harmony.quartz.service.SysJobLogService;
import com.lvbaoins.Harmony.sys.core.exception.enums.BizExceptionEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 定时任务日志控制器
 *
 * @author gjb
 * @Date 2020-09-15 14:17:29
 */
@Controller
@RequestMapping("/sysJobLog")
public class SysJobLogController extends BaseController {

    private String PREFIX = "/sysJobLog/";

    @Autowired
    private SysJobLogService sysJobLogService;

    /**
     * 跳转到定时任务日志首页
     */
    @RequestMapping("")
    public String index(Long sysJobId, ModelMap modelMap) {
        modelMap.put("sysJobId", sysJobId);
        return PREFIX + "sysJobLog.html";
    }

    /**
     * 跳转到添加定时任务日志
     */
    @RequestMapping("/sysJobLog_add")
    public String sysJobLogAdd() {
        return PREFIX + "sysJobLog_add.html";
    }

    /**
     * 跳转到修改定时任务日志
     */
    @RequestMapping("/sysJobLog_edit")
    public String sysJobLogUpdate(String sysJobLogId) {
        return PREFIX + "sysJobLog_edit.html";
    }

    /**
     * 跳转到详情定时任务日志
     */
    @RequestMapping("/sysJobLog_info")
    public String sysJobLogInfo(String sysJobLogId) {
        return PREFIX + "sysJobLog_info.html";
    }

    /**
     * 获取定时任务日志列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(Integer page, Integer limit, SysJobLog sysJobLog) {
        IPage<SysJobLog> sysJobLogPage = sysJobLogService.pageList(page, limit, sysJobLog);
        return LayuiPageFactory.createPageInfo(sysJobLogPage);
    }

    /**
     * 新增定时任务日志
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public ResponseData add(SysJobLog sysJobLog) {
        sysJobLogService.save(sysJobLog);
        return SUCCESS_TIP;
    }

    /**
     * 删除定时任务日志
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public ResponseData delete() {
        boolean remove = this.sysJobLogService.remove(null);
        if(remove){
            return SUCCESS_TIP;
        } else {
            return new ErrorResponseData("删除失败");
        }
    }

    /**
     * 修改定时任务日志
     */
    @RequestMapping(value = "/edit")
    @ResponseBody
    public ResponseData edit(SysJobLog sysJobLog) {
        sysJobLogService.updateById(sysJobLog);
        return SUCCESS_TIP;
    }

    /**
     * 查看定时任务日志
     *
     * @author gjb
     */
    @RequestMapping(value = "/view/{sysJobLogId}")
    @ResponseBody
    public ResponseData view(@PathVariable Long sysJobLogId) {
        if (ToolUtil.isEmpty(sysJobLogId)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        SysJobLog sysJobLog = this.sysJobLogService.getById(sysJobLogId);

        return ResponseData.success(sysJobLog);
    }

    /**
     * 定时任务日志详情
     */
    @RequestMapping(value = "/detail/{sysJobLogId}")
    @ResponseBody
    public ResponseData detail(@PathVariable("sysJobLogId") Integer sysJobLogId) {
        return ResponseData.success(sysJobLogService.getById(sysJobLogId));
    }
}
